package com.chenye.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * @Auther:chenye
 * @Data:2018/4/11
 * @Description:
 */
@Configuration
@EnableConfigurationProperties(TestProperties.class)
@ConditionalOnClass(TestService.class)
@ConditionalOnProperty(name = "test",havingValue = "123")
@Import(TestImportConfiguration.class)
public class TestServiceAutoConfiguration {

    private TestProperties testProperties;

    public TestServiceAutoConfiguration(TestProperties testProperties) {
        this.testProperties = testProperties;
    }

    @Bean
    @ConditionalOnMissingBean(TestService.class)
    public TestService testService(){
        TestService testService = new TestService();
        testService.setName(testProperties.getName());
        testService.setAge(testProperties.getAge());
        testService.setNodeAddress(testProperties.getNodeAddress());
        return testService;
    }
}
